package com.chenx.myapplicationnav.ui.home.db

import android.content.Context
import androidx.room.Database
import androidx.room.Room.databaseBuilder
import androidx.room.RoomDatabase


@Database(entities = [User::class], version = 2)

    abstract class AppDatabase : RoomDatabase() {

    abstract fun userDao(): UserDao?

    // 单例
companion object{
        private var database: AppDatabase? = null
        fun getInstance(context: Context): AppDatabase? {
        if (database == null) {
            synchronized(AppDatabase::class.java) {
                if (database == null) {
//                    database = databaseBuilder(
//                        context.applicationContext,
//                        AppDatabase::class.java,
//                        "user.db"
//                    ).build()

                    database =    databaseBuilder(
                        context.applicationContext,
                        AppDatabase::class.java, "user.db"
                    ).allowMainThreadQueries().build()

                }
            }
        }
        return database
    }
}


}

